package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.RealAuth;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleMapper;
import cn.wolfcode.p2p.business.query.BidReturnQuery;
import cn.wolfcode.p2p.business.service.IPaymentScheduleService;
import cn.wolfcode.p2p.business.service.IpaymentScheduleDetailService;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.PageResult;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PaymentScheduleServiceImpl implements IPaymentScheduleService {
        @Autowired
        private PaymentScheduleMapper paymentScheduleMapper;
        @Autowired
        private IpaymentScheduleDetailService paymentScheduleDetailService;
        /**
         * 保存每期的还款计划
         * @param br
         */
        @Override
        public void createPaymentSchedule(BidRequest br) {
                int monthIndexs = br.getMonthes2Return();
                Date beginDate = br.getPublishTime();//第一期还款开始时间
                for(int i=0;i<monthIndexs;i++){
                        //为每一期生成还款计划
                        PaymentSchedule ps = new PaymentSchedule();
                        int monthIndex= i+1;
                        ps.setDeadline(DateUtils.addMonths(beginDate,monthIndex));//开始时间加一个月
                        /*ps.setPayDate(new Date());实际还款在还款时设置*/
                        ps.setMonthIndex(monthIndex);//第几期
                        BigDecimal monthToReturnMoney = CalculatetUtil.calMonthToReturnMoney(br.getReturnType(), br.getBidRequestAmount(), br.getCurrentRate(),
                                monthIndex, br.getMonthes2Return());
                        ps.setTotalAmount(monthToReturnMoney.setScale(Constants.SCALE_STORE, RoundingMode.HALF_UP));//当期还款总额
                        BigDecimal monthlyInterest = CalculatetUtil.calMonthlyInterest(br.getReturnType(), br.getBidRequestAmount(), br.getCurrentRate()
                                , monthIndex, br.getMonthes2Return());
                        ps.setInterest(monthlyInterest.setScale(Constants.SCALE_STORE, RoundingMode.HALF_UP));//当期还款利息
                        ps.setPrincipal(monthToReturnMoney.subtract(monthlyInterest));//当期还款本金
                        /*ps.setState(0); 默认待还款*/
                        ps.setBidRequestType(br.getBidRequestType());
                        ps.setReturnType(br.getReturnType());
                        ps.setBidRequestId(br.getId());
                        ps.setBorrowUser(br.getCreateUser());
                        ps.setBidRequestTitle(br.getTitle());
                        //保存一期计划
                        paymentScheduleMapper.insert(ps);
                        paymentScheduleDetailService.createPaymentScheduleDetail(ps,br);


                }
        }

        @Override
        public PageResult selectForList(BidReturnQuery qo) {
                 //先查出COunt,如果COUNT为0,返回空集合
                 int row = paymentScheduleMapper.selectCount(qo);
                 if(row==0){
                     return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
                 }
                 List<RealAuth> list =paymentScheduleMapper.query(qo);
                 return new PageResult(list,row,qo.getCurrentPage(),qo.getPageSize());
        }

        @Override
        public PaymentSchedule getById(Long id) {
                return paymentScheduleMapper.selectByPrimaryKey(id);
        }

        @Override
        public void update(PaymentSchedule ps) {
                paymentScheduleMapper.updateByPrimaryKey(ps);
        }

        @Override
        public int getReturnSuccessMonthIndexNumber(Long bidRequestId, int state) {

                return paymentScheduleMapper.getReturnSuccessMonthIndexNumber(bidRequestId,state);
        }
}
